package com.tianya.controller;
import com.tianya.common.Results;
import com.tianya.domain.dto.MessageQueryDTO;
import com.tianya.domain.dto.MessageSendDTO;
import com.tianya.domain.vo.MessageVO;
import com.tianya.domain.vo.PageResultVO;
import com.tianya.domain.vo.UnreadCountVO;
import com.tianya.service.MessageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api("消息管理")
@RestController
@RequestMapping("/message")
@Validated
public class MessageController {

    @Autowired
    private MessageService messageService;

    @ApiOperation("获取收件箱消息")
    @GetMapping("/inbox")
    public Results<PageResultVO<MessageVO>> getInboxMessages(
            @RequestParam(defaultValue = "1") Long pageNum,
            @RequestParam(defaultValue = "10") Long pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Boolean isRead,
            @RequestParam Long userId) {  // 添加userId参数
        MessageQueryDTO queryDTO = new MessageQueryDTO();
        queryDTO.setPageNum(pageNum);
        queryDTO.setPageSize(pageSize);
        queryDTO.setKeyword(keyword);
        queryDTO.setIsRead(isRead);

        return Results.success(messageService.getInboxMessages(userId, queryDTO));
    }

    @ApiOperation("发送消息")
    @PostMapping("/send")
    public Results<Boolean> sendMessage(
            @RequestBody @Valid MessageSendDTO messageDTO,
            @RequestParam Long userId) {  // 添加userId参数
        return Results.success(messageService.sendMessage(userId, messageDTO));
    }

    @ApiOperation("标记消息为已读")
    @PutMapping("/{messageId}/read")
    public Results<Boolean> markAsRead(
            @PathVariable Long messageId,
            @RequestParam Long userId) {  // 添加userId参数
        return Results.success(messageService.markAsRead(messageId, userId));
    }

    @ApiOperation("删除消息")
    @DeleteMapping("/{messageId}")
    public Results<Boolean> deleteMessage(
            @PathVariable Long messageId,
            @RequestParam Long userId) {  // 添加userId参数
        return Results.success(messageService.deleteMessage(messageId, userId));
    }

    @ApiOperation("获取未读消息数量")
    @GetMapping("/unread/count")
    public Results<UnreadCountVO> getUnreadCount(
            @RequestParam Long userId) {  // 添加userId参数
        return Results.success(messageService.getUnreadCount(userId));
    }

    @ApiOperation("获取消息历史")
    @GetMapping("/history/{otherUserId}")
    public Results<List<MessageVO>> getMessageHistory(
            @PathVariable Long otherUserId,
            @RequestParam Long userId) {  // 添加userId参数
        return Results.success(messageService.getMessageHistory(userId, otherUserId));
    }
    @ApiOperation("获取消息详情")
    @GetMapping("/{messageId}/detail")
    public Results<MessageVO> getMessageDetail(
            @PathVariable Long messageId,
            @RequestParam Long userId) {  // 添加userId参数
        return Results.success(messageService.getMessageDetail(messageId, userId));
    }
}